/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Authenticate;

import Module.ModuleBean;
import SubModule.SubModuleBean;
import User.UserBean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;




public class Query {
    
    static Connection con=null;
    
public static int AuthenticateLogin(String user,String pass) throws SQLException
{
    int i=0;
    try
    {
        con=DBConnection.Connection.getConnection();
        PreparedStatement pre=con.prepareStatement("select user_id from users where email=? and password=?");
        pre.setString(1,user);
        pre.setString(2, pass);
        ResultSet rs=pre.executeQuery();
        if(rs.next())
        {
                i=rs.getInt(1);
        }
        else
            {
                i=0;
            }
    }
    catch(Exception e)
    {
        return i;
    }
    finally
    {
        con.close();
    }
    return i;
}

public static boolean smartLogin() throws SQLException
{
    boolean i=false;
    try
    {
        con=DBConnection.Connection.getConnection();
        PreparedStatement pre=con.prepareStatement("select count(*) from users where user_type=?");
        pre.setString(1,"admin");
        ResultSet rs=pre.executeQuery();
        if(rs.next())
        {
            if(rs.getInt(1)>0)
            {
                i=true;
            }
            else
            {
                i=false;
            }
        }
    }
    catch(Exception e)
    {
        return i;
    }
    finally
    {
        con.close();
    }
    return i;
}

public static int add(String user,String pass) throws SQLException
{
    int i=0;
    try
    {
      con=DBConnection.Connection.getConnection();
      PreparedStatement pre=con.prepareStatement("insert into users values (userid.nextval,sysdate,?,?,?,?,?,sysdate,?,?)");
      pre.setString(1,pass);
      pre.setString(2,"admin");
      pre.setString(3,user);
      pre.setString(4,"Romus");
      pre.setDate(5, null);
      pre.setInt(6,1);
      pre.setString(7,user);
      i=pre.executeUpdate();
      if(i!=0)
      {
              i=addAccess();
      }
    }
    catch(Exception e)
    {
        //System.out.println(e);
        return i;
    }
    finally
    {
        con.close();
    }
    return i;
}

public static int addAccess()
{
    int i=0,id=0;
    try
    {
        id=getMaxId();
        PreparedStatement pre=con.prepareStatement("insert into user_access_permission values(useraccessid.nextval,?,?,?,?,sysdate)");
        PreparedStatement pre1=con.prepareStatement("select * from sub_module order by sub_module_id");
        ResultSet rs=pre1.executeQuery();
        while(rs.next())
        {
          pre.setInt(1,id);
          pre.setInt(2,rs.getInt(3));
          pre.setInt(3,rs.getInt(1));
          pre.setInt(4,1);
          pre.addBatch();
        }
       int k[]=pre.executeBatch();
       if(k.length>0)
       {
           i=1;
       }
       else
       {
           i=0;
       }
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
    return i;
}
public static int getMaxId()
{
     int i=0;
    try
    {
      PreparedStatement pre=con.prepareStatement("select max(user_id) from users");
      ResultSet rs=pre.executeQuery();
      if(rs.next())
      {
          i=rs.getInt(1);
      }
    }
    catch(Exception e)
    {
       return i; 
    }
    return i;
}
public static UserBean AuthenticateStuLogin(String user) throws SQLException
{
    UserBean ub=new UserBean();
    try
    {
        con=DBConnection.Connection.getConnection();
        PreparedStatement pre=con.prepareStatement("select EMAIL, USER_TYPE, EMPLOYEE_NAME from users where email=?");
        pre.setString(1,user);
        ResultSet rs=pre.executeQuery();
        if(rs.next()){
                ub.setEmail(rs.getString(1));
                ub.setUserType(rs.getString(2));
                ub.setEmployeeName(rs.getString(3));
        }
    }
    catch(Exception e)
    {
        return ub;
    }
    finally
    {
        con.close();
    }
    return ub;
}
}
